#!/bin/bash
# Authored : <fanyuantao>
# Description: This script is used to set firewall rules for the base model.

function add_firewall_rules() {
    echo "example single port: 8080"
    echo "example multiple ports: 8080,8081,8082"
    echo "backSpace operation: 0"
    while true; do
    read -p "Please input you want to add firewall rules : " server_port
        if [[ -z $server_port ]] || [[ $server_port == 0 ]]; then
            echo "Cancel operation"
            set_firewall
        elif  [[ -z $server_port ]] || [[ $server_port =~ ^[0-9]+$ ]]; then
            firewall-cmd --zone=public --add-port=$server_port/tcp --permanent -q
            firewall-cmd --reload
            break
        elif  [[ -z $server_port ]] || [[ $server_port =~ ^[0-9]+(,[0-9]+)*$ ]]; then
            for port in $(echo $server_port | tr ',' '\n'); do
                firewall-cmd --zone=public --add-port=$port/tcp --permanent -q
            done
            firewall-cmd --reload
            break
        else
            echo "Error input,Please input again :"
        fi
    done
    echo "Current firewall rules:"
    firewall-cmd --zone=public --list-ports
}

function delte_firewall_rules() {
    echo "example single port: 8080"
    echo "example multiple ports: 8080,8081,8082"
    echo "backSpace operation: 0"
    while true; do
    read -p "Please input you want to delete firewall rules : " server_port
        if [[ -z $server_port ]] || [[ $server_port == 0 ]]; then
            echo "Cancel operation"
            set_firewall
        elif  [[ -z $server_port ]] || [[ $server_port =~ ^[0-9]+$ ]]; then
            firewall-cmd --zone=public --remove-port=$server_port/tcp --permanent -q
            firewall-cmd --reload
            break
        elif  [[ -z $server_port ]] || [[ $server_port =~ ^[0-9]+(,[0-9]+)*$ ]]; then
            for port in $(echo $server_port | tr ',' '\n'); do
                firewall-cmd --zone=public --remove-port=$port/tcp --permanent -q
            done
            firewall-cmd --reload
            break
        else
            echo "Error input,Please input again :"
        fi
    done
    echo "Current firewall rules:"
    firewall-cmd --zone=public --list-ports
}

function list_firewall_rules() {
    echo "Current firewall rules :"
    firewall-cmd --zone=public --list-ports
}

function open_firewall() {
    if systemctl status firewalld | grep -q "inactive"; then
        systemctl start firewalld && systemctl enable firewalld
    fi
    echo "success"
}

function close_firewall() {
    if systemctl status firewalld | grep -q "active"; then
        systemctl disable --now firewalld
    fi
    echo "success"
}

function look_firewall_status() { 
    systemctl status firewalld | grep -q "Active: active" && echo "firewalld is active" || echo "firewalld is inactive"
}

function set_firewall() {
    while true; do
        echo ""
        echo "1. Open firewall"
        echo "2. Close firewall"
        echo "3. Look status of firewall"
        echo "4. Add firewall rules"
        echo "5. Delete firewall rules"
        echo "6. List firewall rules"
        echo "0. Exit"
        echo ""
        read -p "Please select : " choice
        case $choice in
            1) open_firewall;;
            2) close_firewall;;
            3) look_firewall_status;;
            4) add_firewall_rules;;
            5) delte_firewall_rules;;
            6) list_firewall_rules;;
            0) exit;;
            *) echo "Invalid choice, please try again.";;
        esac
    done
}

function check_firewalld_status() {
    if systemctl status firewalld | grep -q "inactive"; then
        systemctl start firewalld
    fi
}

function main() {
    check_firewalld_status
    set_firewall
}

main